<?php

/**
 * 上海创蓝
 * @author ben <ben@spicatech.cn>  Created on 2015-7-26
 */
class SMSCL extends SMSBase implements SMSInterface {

    protected $apiUrl = 'http://222.73.117.158/msg/HttpBatchSendSM';
    private static $instances = null;

    /**
     * 获取实例id
     * @param string $host
     * @param int $port
     * @param string $user
     * @param string $pwd
     * @return string
     */
    public static function getInstanceId($user = '', $pwd = '') {
        return md5($user . $pwd);
    }

    /**
     * 获取实例
     * @param string $user
     * @param string $pwd
     * @param string $sign
     * @return Email
     */
    public static function getInstance($user = '', $pwd = '', $sign = null) {
        $id = self::getInstanceId($user, $pwd);
        if (!isset(self::$instances[$id]) || !is_a(self::$instances[$id], 'SMS')) {
            self::$instances[$id] = new self($user, $pwd, $sign);
        }
        $sms = self::$instances[$id];
        return $sms;
    }

    public function send($mobile, $msg) {
        $params = array(
            'account' => $this->user,
            'pswd' => $this->pwd,
            'mobile' => $mobile, // 手机号 多个用英文的逗号隔开 100个/次
            'msg' => $msg . $this->sign, // 短信内容
            'needstatus' => false
        );
        $http = HTTPClient::getInstance();
        $res = $http->get($this->apiUrl, $params);
        $code = $this->parseResult($res);
        
        if ($code != 0) {
            throw new Exception($this->getErrorMessage($code), $code);
        }
        return $code;
    }

    public function getErrorMessage($errorCode) {
        switch ($errorCode) {
            case 0:
                $msg = '成功';
                break;
            case 101:
                $msg = '无此用户';
                break;
            case 102:
                $msg = '密码错';
                break;
            case 103:
                $msg = '提交过快（提交速度超过流速限制）';
                break;
            case 104:
                $msg = '系统忙（因平台侧原因，暂时无法处理提交的短信）';
                break;
            case 105:
                $msg = '敏感短信（短信内容包含敏感词';
                break;
            case 106:
                $msg = '消息长度错（>536或<=0）';
                break;
            case 107:
                $msg = '包含错误的手机号码';
                break;
            case 108:
                $msg = '手机号码个数错（群发>50000或<=0;单发>200或<=0）';
                break;
            case 109:
                $msg = '无发送额度（该用户可用短信数已使用完）';
                break;
            case 110:
                $msg = '不在发送时间内';
                break;
            case 111:
                $msg = '超出该账户当月发送额度限制';
                break;
            case 112:
                $msg = '无此产品，用户没有订购该产品';
                break;
            case 113:
                $msg = 'extno格式错（非数字或者长度不对）';
                break;
            case 115:
                $msg = '自动审核驳回';
                break;
            case 116:
                $msg = '签名不合法，未带签名（用户必须带签名的前提下）';
                break;
            case 117:
                $msg = 'IP地址认证错,请求调用的IP地址不是系统登记的IP地址';
                break;
            case 118:
                $msg = '用户没有相应的发送权限';
                break;
            case 119:
                $msg = '用户已过期';
                break;
            default :
                $msg = $errorCode;
        }
        return $msg;
    }

    /**
     * 
     * @param type $res
     * @return type
     */
    private function parseResult($res) {
        $arr = explode("\n", $res);
        if (empty($arr)) {
            return -100;
        }
        return intval(explode(',', $arr[0])[1]);
    }

}
